
import  wx
import serial.tools.list_ports
import serial


class myframe2(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"88888888",size=(100,80),pos=(30,30))
        panel = wx.Panel(self)


class myframe1(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"myframe",size=(500,380),pos=(10,10))
        panel = wx.Panel(self)

        self.exitbutton = wx.Button(panel, -1, "EXIT", size=(60, 30), pos=(400, 300))

        self.textboxWrite=wx.TextCtrl(panel,-1,"",size=(100,100),pos=(100,100))
        self.sentbutton=wx.Button(panel,-1,"sent",size=(80,50),pos=(110,215))

        self.textboxRead = wx.TextCtrl(panel, -1," ", size=(100, 100), pos=(250, 100))
        self.readbutton = wx.Button(panel, -1, "Read", size=(80, 50), pos=(260, 215))

        plist = list(serial.tools.list_ports.comports())
        list2 = []
        if len(plist) <= 0:
            print("没有发现端口!")
        else:
            for i in range(len(plist)):
                plist_0 = list(plist[i])
                print(plist_0[0])
                list2.append(plist_0[0])

            serialName = plist_0[0]
            serialFd = serial.Serial(serialName, 9600, timeout=60)
            print("可用端口名>>>", serialFd.name)
            print(list2)


        self.listbox4 = wx.Choice(panel, -1, (120, 50), (200, 40),list2)

        self.exitbutton.Bind(wx.EVT_BUTTON, self.exit)
        self.sentbutton.Bind(wx.EVT_BUTTON,self.sentclick)
        # self.readbutton.Bind(wx.EVT_BUTTON, self.readclick)

    def exit(self,event):
         self.Destroy()


    def sentclick(self,event):

        #打开串口
        serialPort=self.listbox4.GetStringSelection()  #串口
        baudRate=9600       #波特率
        ser=serial.Serial(serialPort,baudRate,timeout=0.5)
        print ("参数设置：串口=%s ，波特率=%d"%(serialPort,baudRate))

        #收发数据
        str=self.textboxWrite.Value
        ser.write((str+'\n').encode())
        message = ser.readline()
        print(message)                      #可以接收中文
        self.textboxRead.Value = message
        ser.close()

app = wx.App()
yyy = myframe1()
yyy.Show()
app.MainLoop()
